import Cookies from 'js-cookie'
import Config from '@/config/index'
import { Commit } from 'vuex'

export interface State {
  sidebar: {
    opened: boolean,
    withoutAnimation: boolean,
  }
  device: string
  defaultImage: any
}

const initState: State = {
  sidebar: {
    opened: !+Cookies.get('sidebarStatus'),
    withoutAnimation: false,
  },
  device: 'desktop',
  // 默认图片
  defaultImage: Config.defaultImage,
}

// getters
const mutations = {
  TOGGLE_SIDEBAR: (state: State) => {
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
  },
  CLOSE_SIDEBAR: (state: State, withoutAnimation: boolean) => {
    Cookies.set('sidebarStatus', 1)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state: State, device: string) => {
    state.device = device
  },
}

// actions
const actions = {
  ToggleSideBar: (context: { commit: Commit; state: State }) => {
    context.commit('TOGGLE_SIDEBAR')
  },
  CloseSideBar(context: { commit: Commit; state: State }, data: { withoutAnimation: boolean }) {
    context.commit('CLOSE_SIDEBAR', data.withoutAnimation)
  },
  ToggleDevice(context: { commit: Commit; state: State }, device: string) {
    context.commit('TOGGLE_DEVICE', device)
  },
}

export default {
  state: initState,
  actions,
  mutations,
}
